package com.resource.effect
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	/**
	 * swf格式的特效
	 * 能做缓存吗，这里暂时不考虑缓存
	 * 直接继承Loader减少显示层数，有用吗？
	 * 
	 * 会有帧率降低问题
	 */
	public class SwfEffect extends Loader
	{
		private var _url:String;
		private var reLoad:Boolean = true;
		
		public function SwfEffect(url:String)
		{
			super();
			_url = url;
			reLoad = true;
		}

		public function play():void{
			if(reLoad){
				contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
				contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
				load(new URLRequest(url));
				reLoad = false;
			}else{
				if(content){
					(content as MovieClip).gotoAndPlay(0);
				}
			}
		}
		
		private function onLoaded(event:Event):void{
			contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
			contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			(content as MovieClip).gotoAndPlay(0);
		}
		
		private function onError(event:IOErrorEvent):void{
			contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
			contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
		}
		
		public function get url():String
		{
			return _url;
		}
		
		public function set url(value:String):void
		{
			if(_url != value){
				_url = value;
				reLoad = true;
			}
		}

	}
}